define(function(require, exports, module)
{

    var $ = require('$');
    require("bootstrap_commonrequire");
    var bootbox = require('bootbox');

    var formcc = require('form');
    var store = require("store");
    var userinfo = store.get('userInfo');
    var theme = require("bootstrap_theme");

    exports.init = function(back)
    {
        var html = "<div class='span12' style='margin-left:0'><div class='row-fluid'><div class='span12'><div class='page-header'><h1 class='pull-left'><i class='icon-cog'></i><span>修改密码</span></h1></div></div></div><div id='changepwd'></div></div>";
        var oldhtml = $("#content-wrapper").html();
        $("#content-wrapper").html(html);
        isSelect2 = false;
        var mybuttons = [
        {
            text : "保存",
            id : "btn_mct_save",
            type : "submit",
            style : "primary",
            name : ""

        }];
        if (back)
        {
            mybuttons = [
            {
                text : "保存",
                id : "btn_mct_save",
                type : "submit",
                style : "btn btn-contrast",
                name : ""

            },
            {
                text : "取消",
                id : "btn_mct_cancel",
                style : "btn",
                type : "button"
            }];
        }
        new formcc(
        {
            attrs :
            {
                title : "修改密码",
                spansize : 'span12',
                id : "pwdform",
                items : [
                {
                    isText : true,
                    label : "原始密码",
                    id : 'oldpwd',
                    name : 'oldpwd',
                    type : "password",
                    style : "width:50%",
                    validations :
                    {
                        required : true,
                        minlength : 6
                    },
                },
                {
                    isText : true,
                    label : "新密码",
                    id : 'pwd',
                    name : 'pwd',
                    type : "password",
                    style : "width:50%",
                    validations :
                    {
                        required : true,
                        minlength : 6
                    },
                },
                {
                    isText : true,
                    label : "新确认密码",
                    id : 'checkpwd',
                    name : 'checkpwd',
                    type : "password",
                    style : "width:50%",
                    validations :
                    {
                        required : true,
                        equalto : "pwd"
                    },
                }],
                buttons : mybuttons

            },
            renderTo : 'changepwd',
            afterRender : function()
            {
                $('#btn_mct_cancel').on('click', function()
                {
                    $("#content-wrapper").html(oldhtml);
                });
                theme.setValidateForm($("#pwdform"), function()
                {
                    var formdata =
                    {
                        pwd : $("#pwd").val(),
                        checkpwd : $("#checkpwd").val(),
                        oldpwd : $("#oldpwd").val(),
                        userid : userinfo.userId
                    };

                    $.ajax(
                    {
                        url : "/menu/changePwd.do", //GLOBAL.URL.UPDATEPWD,
                        dataType : "json",
                        data : "data=" + JSON.stringify(formdata),
                        type : "POST",
                        success : function(data)
                        {
                            if (!data.retFlag)
                            {
                                bootbox.alert(data.retMsg);
                                return;
                            }
                            else
                            {
                                if (back)
                                    $("#content-wrapper").html(oldhtml);
                                else
                                    $("#content-wrapper").html("");
                                bootbox.alert("修改成功");
                            }
                        },
                        error : function()
                        {
                            bootbox.alert("网络请求失败，请重试！");
                        }
                    });
                });
            }
        });
    };
});
